<?php
include 'library.php'; // include the library for database connection

function encrypt($string){
	return base64_encode(base64_encode(base64_encode($string)));
}

function decrypt($string){
	return base64_decode(base64_decode(base64_decode($string)));
}

if(isset($_POST['action']) && $_POST['action'] == 'login'){ // Check the action `login`
	$username 		= htmlentities($_POST['username']); // Get the username
	$password 		= htmlentities(encrypt($_POST['password'])); // Get the password and decrypt it
	$query			= mysql_query('SELECT * FROM User WHERE username = "'.$username.'" AND password = "'.$password.'" '); // Check the table with posted credentials
	$num_rows		= mysql_num_rows($query); // Get the number of rows
	
	include_once 'config.php';
	
	$yhendus=new mysqli($host, $user, $pw, $db);
	$kask=$yhendus->prepare("SELECT id, username FROM User WHERE username = ? AND password = ?");
	$kask->bind_param("ss", $username, $password);
	$kask->bind_result($id, $username);
	$kask->execute();
	if($kask->fetch()){
		
			$_SESSION['userid'] 	= $id;
			$_SESSION['username'] 	= $username;
			echo 1;
	}else{
		echo 0;
	}

	
}
?>